package gijoe.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import gijoe.dto.AbroadDto;
import gijoe.dto.ApplicationDto;
import gijoe.dto.AwdDto;
import gijoe.dto.BoardNoticeDto;
import gijoe.dto.CareerDto;
import gijoe.dto.CertiDto;
import gijoe.dto.ClubDto;
import gijoe.dto.HopeDto;
import gijoe.dto.InfoDto;
import gijoe.dto.IntroDto;
import gijoe.dto.LangDto;
import gijoe.dto.ParttimeDto;
import gijoe.dto.SubmitDto;
import gijoe.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {
  private UserService service;

  @Autowired
  public void setService(UserService service) {
    this.service = service;
  }
  
  /*---------------------------------------------------공지사항 START*/
  //공지사항 리스트
  @RequestMapping("/notice_list.do")
  public ModelAndView notice_list() {
    System.out.println();
    return new ModelAndView("user.inc1.notice_list", "notice_list", service.noticeSelectAll());
        
  }
  //공지사항 상세
  @RequestMapping("/notice_detail.do")
  public ModelAndView noticeSelectOne(@RequestParam String notice_num) {
    System.out.println(notice_num);
    return new ModelAndView("user.inc1.notice_detail", "dto", service.noticeSelectOne(notice_num));
  }
  
  
  //----채용--------------------------------------
  @RequestMapping("/recruit.do")
  public ModelAndView recruit(){
    return new ModelAndView("user.inc.recruitment");
  }
  
  @RequestMapping("/recruitment_detail.do")
  public ModelAndView recruitment_detail(@RequestParam String rcrt_num) {
    return new ModelAndView("user.inc.recruitment_detail", "dto",
        service.recruitSelectOne(rcrt_num));
  }
    
  @RequestMapping("/recruitment_newcomer.do")
  public ModelAndView newcomer(){
    return new ModelAndView("user.inc.recruitment_newcomer", "list", service.recruitSelectAll());
  }
  
  @RequestMapping("/recruitment_career.do")
  public ModelAndView career(){
    return new ModelAndView("user.inc.recruitment_career", "list", service.recruitSelectAll());
  }
  
  // 지원자가 삭제 진행했을 경우
    @RequestMapping("/deleteEnter.do")
    public ModelAndView recruitDelete(String rcrt_num, String app_nm, String email, String email_addr, String app_pwd) {
      email += ("@" + email_addr);
      //System.out.println(email);
      //System.out.println(app_nm);
      System.out.println(rcrt_num + "/" + app_nm + "/" + email + "/" + app_pwd);
      service.recruitDelete(rcrt_num ,app_nm,email,app_pwd);
      return new ModelAndView("user.inc.app_delete");
    }
    
    // 지원자가 삭제페이지 요청시 불러올 채용공고 목록들
    @RequestMapping("/app_delete.do")
    public ModelAndView recruitList() {
      return new ModelAndView("user.inc.app_delete","recList",service.recruitList());
    }
    //지원서 접수 insert
    @RequestMapping("/app_basic_insert.do")
    public ModelAndView basic(ApplicationDto dto, HttpSession ss) {
      System.out.println(dto.toString());
      System.out.println(dto.getBirth());
      
      int result = service.applicainsert(dto);
    
      if (result == 1) {
        System.out.println("성공");
        ss.setAttribute("app_num", dto.getApp_num());
        ss.setAttribute("app_nm", dto.getApp_nm());
        String birth = dto.getBirth();
        String birthyy = birth.substring(0, 4);
        String birthmm = birth.substring(5, 7);
        String birthdd = birth.substring(8, 10);
        ss.setAttribute("birthyy", birthyy);
        ss.setAttribute("birthmm", birthmm);
        ss.setAttribute("birthdd", birthdd);
        ss.setAttribute("email", dto.getEmail());
      } else {
        System.out.println("insert실패");
      }
      return new ModelAndView("user.inc.app_basic_insert");
    }
    
    //지원서페이지공고번호 가져오기
    @RequestMapping("/app_receipt_insert.do")
    public ModelAndView receipt() {
      return new ModelAndView("user.inc.app_receipt_insert", "list",
          service.app_receiptSelectAll());
    }
    
  /*---------------------------------------------------합격자발표 START*/
  //합격자발표 페이지 로딩
  @RequestMapping("/app_pass_form.do")
  public ModelAndView app_pass_form() {
    return new ModelAndView("user.inc.app_pass_form", "pass", service.userPassForm());
  }
  
  @RequestMapping("/app_pass_search.do")
  public ModelAndView app_pass_search(@RequestParam String rcrt_num, String recruit_cd, String app_nm, 
                    String birth_yy, String birth_mm, String birth_dd, 
                    String email_id, String email_addr, String app_pwd) {
    return new ModelAndView("user.inc.app_pass_search", "pass_user", 
                service.userPassSearch(rcrt_num, recruit_cd, app_nm, birth_yy, birth_mm, birth_dd, email_id, email_addr, app_pwd));
  }
  /*---------------------------------------------------합격자발표 End*/
  
  
/*  
   파일 업로드 ajax 
  
  @RequestMapping(value="/fileUploadAjax.do", method=RequestMethod.POST)
  public ModelAndView fileUploadAjax(MultipartHttpServletRequest mRequest) {
    System.out.println("aaaaa");
    service.fileUpload(mRequest);
    ModelAndView mav = new ModelAndView();
    if(service.fileUpload(mRequest)) {
      mav.addObject("result", "success");
    } else {
      mav.addObject("result", "fail");
    }
    mav.setViewName("user.inc.app_basic_insert");
    return mav ;
  }*/
  
  /*---------------------------------------------------지원서접수 START*/
  //기본사항페이지 insert
    @ResponseBody   
    @RequestMapping("/app_basic_insertok.do")
    public  Map<String, Object> app_basic_insertok( InfoDto dto,String app_num,String sch_cd,
        String sch_fr_ymm,String sch_to_ymm, String sch_ent_cd,String sch_nm,String sch_mjr_cd,
        String sch_mjr_nm,String sch_gra_cd,String sch_mark,String sch_fl_mark,String sch_grad_yn,HttpServletRequest request) throws IOException{
      
      System.out.println(dto.toString());
      String sch_fr_ym= sch_fr_ymm;
      String sch_to_ym= sch_to_ymm;
      Map<String, Object> map=new HashMap<String, Object>();
      
      System.out.println(app_num+"/"+ sch_cd+"/"+ sch_fr_ym+"/" +sch_to_ym+"/" +sch_ent_cd+"/"+ sch_nm+"/" +sch_mjr_cd+"/"+ 
          sch_mjr_nm+"/"+ sch_gra_cd+"/" +sch_mark+"/" +sch_fl_mark+"/"+ sch_grad_yn);
      
      int Inforesult=service.Infoinsert(dto);
      System.out.println(Inforesult);
      int Schresult=service.Schinsert(app_num, sch_cd, sch_fr_ym, sch_to_ym, sch_ent_cd, sch_nm, sch_mjr_cd, 
          sch_mjr_nm, sch_gra_cd, sch_mark, sch_fl_mark, sch_grad_yn);
      System.out.println(Schresult);
      if(Inforesult==1&Schresult==1){
        map.put("key", "success");
      }else{
        map.put("key", "fail");
      }
      return map;
    }
    
    // 경력페이지 등록
    @ResponseBody
    @RequestMapping("/app_career_insertok.do")
    public String career_insertok(AbroadDto Abroaddto, CareerDto Careerdto,
        ParttimeDto Parttimedto, ClubDto Clubdto) {
      System.out.println("바보" + Abroaddto.toString());
      System.out.println("메롱" + Careerdto.toString());
      System.out.println(Parttimedto.toString());
      System.out.println(Clubdto.toString());
      String msg = "";
      int Abroadresult = service.AbroadInsert(Abroaddto);
      int Careerresult = service.Careerinser(Careerdto);
      int Parttimeresult = service.Parttimeinsert(Parttimedto);
      int Clubresult = service.Clubinsert(Clubdto);
      if (Abroadresult == 1 & Careerresult == 1 & Parttimeresult == 1
          & Clubresult == 1) {
        msg = "success";
      } else {
        msg = "fail";
      }
      return msg;

    }
    // 기타사항 등록
    @RequestMapping("/app_etc_insertok.do")
    public @ResponseBody String app_etc_insertok(AwdDto Awddto,
        LangDto Langdto, HopeDto Hopedto, CertiDto Certidto,
        HttpServletRequest req) throws UnsupportedEncodingException {
      req.setCharacterEncoding("UTF-8");
      System.out.println(Awddto.toString());
      System.out.println(Langdto.toString());
      System.out.println(Hopedto.toString());
      System.out.println(Certidto.toString());

      String msg = "";
      int Awdresult = service.Awdinsert(Awddto);
      int Langresult = service.Langinsert(Langdto);
      int Hoperesult = service.Hopeinsert(Hopedto);
      int Certiresult = service.Certiinsert(Certidto);
      if (Awdresult == 1 & Langresult == 1 & Hoperesult == 1
          & Certiresult == 1) {
        msg = "success";
      } else {
        msg = "fail";
      }
      return msg;
    }
    
    // 지원접수 자기소개서 등록페이지
    @RequestMapping("/app_self_insert.do")
    public ModelAndView app_self_insert(String app_num) {
      return new ModelAndView("user.inc.app_self_insert", "self",
          service.SelfSelect(app_num));
    }
    //자기소개서 insert
    @RequestMapping("/app_self_insertok.do")
    public @ResponseBody String selfinsert(IntroDto dto) {
      System.out.println(dto.toString());
      String msg = "";
      int result = service.Introinsert(dto);
      System.out.println(result);
      if (result == 1) {
        msg = "success";
      } else {
        msg = "fail";
      }
      return msg;
    }
  
    // 최종제출페이지 공고명가져오기
    @RequestMapping("/app_submit_insert.do")
    public ModelAndView app_submit_insert(String app_num) {
      System.out.println(app_num);
      return new ModelAndView("user.inc.app_submit_insert", "recruit_nm",
          service.Apprecruit_nm(app_num));
    }
    //최종제출 인서트
    @RequestMapping("/submit_insertok.do")
    public ModelAndView submit_insertok(SubmitDto Submitdto ){
      System.out.println(Submitdto.toString());
    int result= service.Submitinster(Submitdto);
    int updateresult=service.Submitupdate(Submitdto);
    System.out.println(result);
    System.out.println(updateresult);
      return new ModelAndView("redirect:main.do");
      
    }
  //지원확인 하기
    @RequestMapping("/identify.do")
    public @ResponseBody Map<String, Object> identify(ApplicationDto dto){
      System.out.println(dto.toString());
      Map<String, Object> map=new HashMap<String, Object>();
    ApplicationDto adto=  service.Appidentify(dto);
    if(adto==null){
      map.put("key", "지원가능합니다");
    }else{
      map.put("key", "이미지원하셨습니다");
    }
      return map;
    
    }
    //지원접수 이메일 확인하기
    @RequestMapping("/emailoverlap.do")
    public @ResponseBody Map<String, Object> emailoverlap(String email,String rcrt_num){
      System.out.println(email+"/"+rcrt_num);
      Map<String, Object> map=new HashMap<String, Object>();
    ApplicationDto dto= service.Emailoverlap(email, rcrt_num);
    if(dto==null){
      map.put("key", "사용가능한 이메일입니다");
    }else{
      map.put("key", "중복된 이메일입니다");
    }
      
      return map;
      
    }
}
